package com.my.vote.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.my.vote.R;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.widget.RxTimeButton;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import io.reactivex.functions.Action;

/**
 * 功能描述:投票详情列表适配器
 *
 * @author Xiao Jingxiang 肖景翔
 * @date 2018/4/12
 */
public class MainAdapter extends BaseQuickAdapter<EVoteInfo, BaseViewHolder> {

    Context context;

    public MainAdapter(Context context) {
        super(R.layout.main_adapter);
        this.context = context;

    }

    @Override
    protected void convert(BaseViewHolder helper, EVoteInfo item) {
        helper.setText(R.id.tvDesc, item.getTitle());
        helper.setText(R.id.tvCandidateSize, "CandidateSize: " + item.getCandidateSize());
        helper.setText(R.id.tvVotingPersonSize, "PersonSize: " + item.getVotingPersonSize());
        helper.setText(R.id.tvVisitSize, "VisitSize: " + item.getVisitSize());
        TextView tvStatus = helper.getView(R.id.tvStatus);
        // 1 投票中， 2 报名中 3 投票结束
        String statue = item.getStatue();

        tvStatus.setText(item.getStatueText(statue));
        tvStatus.setBackgroundResource(item.getStatueDrawable(statue));

        RxTimeButton timeButton = helper.getView(R.id.btTime);

        if (TextUtils.equals("1", statue)) {
            timeButton.startInterval((long) ((item.getVoteEndTime() - System.currentTimeMillis()) / 1000), onComplete);
            timeButton.setTextColor(Color.GREEN);
        } else if (TextUtils.equals("2", statue)) {
            timeButton.startInterval((long) ((item.getEnrollEndTime() - System.currentTimeMillis()) / 1000), onComplete);
            timeButton.setTextColor(Color.RED);
        } else {
            timeButton.stopInterval();
            timeButton.setText("Vote End");
            timeButton.setTextColor(Color.parseColor("#999999"));
        }
        ImageView iv =  helper.getView(R.id.ivURL);
        if (TextUtils.isEmpty(item.getVoteUrl())){
            iv.setImageResource(R.mipmap.blb);
        }else{
            Picasso.with(context).load(item.getVoteUrl()).into(iv);
        }


    }

    Action onComplete = new Action() {
        @Override
        public void run() throws Exception {
            //倒计时结束刷新列表
        }
    };
}
